package com.springstudy.ex;


import com.springstudy.pojo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * @author Tamako
 * @since 2025/1/23 13:17
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ModelAndView runtimeExceptionHandler(RuntimeException e) {
        System.out.println("runtime exception occurred: " + e.getMessage());
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/error1.html");
        return mav;
    }

    @ExceptionHandler(IOException.class)
    @ResponseBody
    public Result ioExceptionHandler(IOException e) {
        System.out.println("runtime exception occurred: " + e.getMessage());
        Result result = new Result();
        result.setCode(500);
        result.setMessage("IOException occurred: " + e.getMessage());
        return result;
    }
}
